package com.example.androidstart.homepage.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

import java.io.IOException;
import java.net.URL;

public class HttpUtil {

    public static void requestImg(final String imgUrl, ImageView imageView) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = null;
                try {

                    bitmap = BitmapFactory.decodeStream(new URL(imgUrl).openStream());

                    showImg(bitmap, imageView);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private static void showImg(final Bitmap bitmap, ImageView imageView) {
        imageView.post(new Runnable() {
            @Override
            public void run() {
                imageView.setImageBitmap(bitmap);
            }
        });
    }
}
